<?php
// +----------------------------------------------------------------------
// | DTPHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://dtphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yyj <qingchen.yyj@gmail.com>
// +----------------------------------------------------------------------


namespace app\admin\controller;


use app\admin\model\CmsCollege;
use app\common\base\BaseController;
use think\Db;

class College extends BaseController
{
    public $table = 'CmsCollege';

    /**
     * 显示资源列表
     *
     * @return array|string
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->requestType();
        $name = $this->request->param('name', '');

        $where = [];

        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }
        $db = Db::name($this->table)->where($where);
        return $this->_list($db);
    }


    public function save()
    {
        $college['college_id'] = $this->request->param('id');
        $college['code'] = $this->request->param('code');
        $college['name'] = $this->request->param('name');
        $college['province'] = $this->request->param('province');
        $college['city'] = $this->request->param('city');
        $college['planOrderName'] = $this->request->param('planOrderName');
        $college['categoryName'] = $this->request->param('categoryName');
        $college['propertyName'] = $this->request->param('propertyName');
        try {
            if ($college['college_id'] == 0) {
                CmsCollege::create($college);
            } else {
                CmsCollege::update($college);
            }
            return $this->buildSuccess([]);
        } catch (\Error $error) {
            return $this->buildFailed();
        }
    }

    public function delete()
    {
        $college_id = $this->request->param('id');
        if (CmsCollege::destroy($college_id)) {
            return $this->buildSuccess([]);
        } else {
            return $this->buildFailed();
        }
    }
}